﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace card
{
    public partial class FK : Form
    {
        public static WebDG.Service ws = new card.WebDG.Service();
        
        public FK()
        {
            InitializeComponent();
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (check_black())
            {
                MessageBox.Show("该车是黑车，不能为其发卡!");
                return;
            }
            int car_no_lenght = this.textBox_carNO.Text.ToString().Trim().Length;
            if (car_no_lenght != 7)
            {
                MessageBox.Show("车号位数不正确!");
                return;
            }
            string car_no_new = this.textBox_carNO.Text.ToString().Trim().ToUpper();
            string card_no_new = this.textBox_card_no.Text.ToString().Trim();
            string get_car = "select XLH,CARDID,CHEHAO from RFID_READ where XLH='" + card_no_new + "' and CARDSTATE='0' and BS is null";
            DataSet ds_car = ws.getDataSet_Serv(get_car);
            string get_car2 = "select XLH,CARDID,CHEHAO from RFID_READ where CHEHAO='" + car_no_new + "' and CARDSTATE='0' and BS is null";
            DataSet ds_car2 = ws.getDataSet_Serv(get_car2);
            if (ds_car2.Tables[0].Rows.Count > 0)//check whether the car has been with one card
            {
                MessageBox.Show("该车已经发卡!");
            }
            else
            {
                if (ds_car.Tables[0].Rows.Count > 0)
                {
                    string car_no = ds_car.Tables[0].Rows[0]["CHEHAO"].ToString().Trim();
                    if (car_no.Length > 0)
                    {
                        MessageBox.Show("该卡已经与指定的车辆关联!");
                    }
                    else
                    {
                        string update_rfid_read = "update RFID_READ set CHEHAO='" + car_no_new + "',SIGNTIME=sysdate where XLH='" + card_no_new + "'";
                        int ret = ws.execute_UID_Serv(update_rfid_read);
                        if (ret == 1)
                        {
                            MessageBox.Show("发卡成功!");
                            this.Close();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("该卡没初始化，请为该卡初始化!");
                }
            }
        }

        private void textBox_card_no_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.textBox_carNO.Focus();
            }
        }

        private void textBox_car_no_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.button_save.Focus();
            }
        }

        //Check the car whether is in black list
        private bool check_black()
        {
            string car_no = this.textBox_carNO.Text.ToString().ToUpper().Trim();
            string get_black_car = "select * from TRUCK_BLACK_NEW where TRUCKNO='" + car_no + "'";
            DataSet ds_black = ws.getDataSet_Serv(get_black_car);
            if (ds_black.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //groupBox3.Left = this.textBox_carNO.Left;
            //groupBox3.Top = this.textBox_carNO.Top + 80;
            groupBox3.Left = this.label2.Left;
            groupBox3.Top = this.label2.Top + 30;
            lab_car_ok.Text = "";
            groupBox3.Visible = true;
            this.textBox_carNO.SelectionStart = this.textBox_carNO.Text.Length;
            this.textBox_carNO.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lab_car_ok.Text = "";
            groupBox3.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox_carNO.Text = "";
            this.textBox_carNO.SelectionStart = this.textBox_carNO.Text.Length;
            this.textBox_carNO.Focus();
        }

        #region
        private void label77_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label77.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label76_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label76.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label75_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label75.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label80_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label80.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label79_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label79.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label82_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label82.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label83_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label83.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label88_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label88.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label87_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label87.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label86_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label86.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label85_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label85.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label84_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label84.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label81_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label81.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label18_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label18.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label17_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label17.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label16_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label16.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label15_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label15.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label14_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label14.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label13_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label13.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label24_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label24.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label23_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label23.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label22_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label22.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label21_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label21.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label20_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label20.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label19_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label19.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label30_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label30.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label29_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label29.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label28_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label28.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label27_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label27.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label26_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label26.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label25_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label25.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label36_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label36.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label35_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label35.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label34_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label34.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label33_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label33.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label32_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label32.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label31_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label31.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label42_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label42.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label41_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label41.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label40_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label40.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label39_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label39.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label38_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label38.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label37_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label37.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label48_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label48.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label47_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label47.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label46_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label46.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label45_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label45.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label44_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label44.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label43_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label43.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label54_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label54.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label53_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label53.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label52_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label52.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label51_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label51.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label50_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label50.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label49_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label49.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label60_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label60.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label59_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label59.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label58_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label58.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label57_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label57.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label56_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label56.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label55_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label55.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label66_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label66.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label65_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label65.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label64_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label64.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label63_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label63.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label62_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label62.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label61_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label61.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label72_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label72.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label71_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label71.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label70_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label70.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label69_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label69.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label68_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label68.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label67_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label67.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }

        private void label78_Click(object sender, EventArgs e)
        {
            textBox_carNO.Text = textBox_carNO.Text + label78.Text;
            textBox_carNO.SelectionStart = textBox_carNO.Text.Length;
            textBox_carNO.Focus();
        }
        #endregion

        private void FK_Load(object sender, EventArgs e)
        {
            //Set the car no select groupbox invisible
            lab_car_ok.Text = "";
            groupBox3.Visible = false;
        }

        private void FK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                //Show or hide the car no input little window
                if (this.groupBox3.Visible == false)
                {
                    this.button1.PerformClick();//perform the click action of btn_car 
                }
                else
                {
                    this.groupBox3.Visible = false;
                }
            }
        }

    }
}
